<!-- 15. 通用组件-猜你喜欢 -->
<script setup lang="ts">
import { getHomeGoodsGuessLikeAPI } from '@/services/home' // 16.1
import type { PageParams } from '@/types/global'
import type { GuessItem } from '@/types/home' // 16.8
import { ref } from 'vue'
import { onMounted } from 'vue'

// 18.end 分页参数 ---衔接19步
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10,
}

// 16.2 获取首页-猜你喜欢数据:
const guessList = ref<GuessItem[]>([]) // 16.9 获取[猜你喜欢]列表项！！！
const finish = ref(false) // 19.2 已结束标记
const getHomeGoodsGuessLikeData = async () => {
  const res = await getHomeGoodsGuessLikeAPI(pageParams)
  // guessList.value = res.result.items
  // 19.3 退出判断
  if (finish.value === true) {
    return uni.showToast({ icon: 'none', title: '已经顶到底啦♥...!' })
  }

  // 19.(分页数据) 数组追加:
  guessList.value.push(...res.result.items)
  // 19.1 页码累加:
  // pageParams.page++ // 细节17: 此时会出现 ts 检索: pageParams.page可能为“未定义”, 因此需加上前缀 - 'Required(当前下为必选)'

  if (pageParams.page < res.result.pages) {
    // pages 最大为 34
    // 页码累加
    pageParams.page++
  } else {
    finish.value = true
  }
}

// 16.3 自定义 "组件挂载" 完毕 ->才去调用对应接口:
onMounted(() => {
  getHomeGoodsGuessLikeData()
})

// 20.7 触发下拉刷新-重置猜你喜欢的数据:
const resetData = () => {
  pageParams.page = 1 // 页码重置为'1'
  guessList.value = [] // 接收接口的列表变量先变空, 后续请求变成第一页的数据
  finish.value = false // 状态为未完成加载的状态
}

// 18.6 暴露获取猜你喜欢列表的方法:
defineExpose({
  resetData, // 20.8 暴露重置[猜你喜欢]的方法
  getMore: getHomeGoodsGuessLikeData, // 细节14: 将方法当作 '名', 然后起一个 '键'
})
</script>

<template>
  <!-- 15.5 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <!-- 16.end 渲染数据 -->
    <navigator
      class="guess-item"
      v-for="item in guessList"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image class="image" mode="aspectFill" :src="item.picture"></image>
      <view class="name">{{ item.name }}</view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{ item.price }}</text>
      </view>
    </navigator>
  </view>
  <!-- 19.end -->
  <view class="loading-text">
    {{ finish ? '已经顶到底啦♥...!' : '正在加载...' }}
  </view>
</template>

<style lang="scss">
:host {
  display: block;
}
/* 15.9 分类标题 */
.caption {
  display: flex;
  justify-content: center;
  line-height: 1;
  padding: 36rpx 0 40rpx;
  font-size: 32rpx;
  color: #262626;
  .text {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 28rpx 0 30rpx;

    &::before,
    &::after {
      content: '';
      width: 20rpx;
      height: 20rpx;
      background-image: url(@/static/images/bubble.png);
      background-size: contain;
      margin: 0 10rpx;
    }
  }
}

/* 猜你喜欢 */
.guess {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx;
  .guess-item {
    width: 345rpx;
    padding: 24rpx 20rpx 20rpx;
    margin-bottom: 20rpx;
    border-radius: 10rpx;
    overflow: hidden;
    background-color: #fff;
  }
  .image {
    width: 304rpx;
    height: 304rpx;
  }
  .name {
    height: 75rpx;
    margin: 10rpx 0;
    font-size: 26rpx;
    color: #262626;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .price {
    line-height: 1;
    padding-top: 4rpx;
    color: #cf4444;
    font-size: 26rpx;
  }
  .small {
    font-size: 80%;
  }
}
// 加载提示文字
.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0;
}
</style>
